<template>
	<view>
		<view class="form">
			<view>
				<view>
					<text class="tit">服务地址</text>
					<text @click="visible = true">{{form.city}}</text>
				</view> 
				<!-- <image src="../../../static/tabbar/a.png" mode="aspectFill"></image> -->
			</view>
			<view>
				<view>
					<text class="tit">详细地址</text>
					<input type="text" v-model="form.address" placeholder="详细地址,如2-2-202"/>
				</view>
			</view>
			<view>
				<view>
					<text class="tit">联系人</text>
					<input type="text" v-model="form.username" placeholder="请输入联系人"/>
				</view>
				<view class="sex">
					<text :class="form.gender == 0?'active':''" @click='form.gender = 0'>男士</text>
					<text :class="form.gender == 1?'active':''" @click='form.gender = 1'>女士</text>
				</view>
			</view>
			<view>
				<view>
					<text class="tit">联系电话</text>
					<input type="text" v-model="form.mobile" placeholder="请输入手机号" maxlength="11"/>
				</view>
				<!-- <image src="../../../static/tabbar/d1.png" mode="aspectFill"></image> -->
			</view>
			<!-- <view>
				<text>设置默认地址</text>
				<image src="../../../static/tabbar/a.png" mode="aspectFill"></image>
			</view> -->
		</view>
		<view class="submit" @click="C_submit">保存</view>
		<w-picker
			:visible.sync="visible"
			mode="region" 
			:current="true"
			fields="day"
			@confirm="onConfirm"
			@cancel="visible = false"
			:disabled-after="false"
		></w-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				visible:false,
				form:{
					city:'点击选择地址',
					address:'',
					username:'',
					mobile:'',
					gender:0,
					// isDef:false     1是  2 否
				}
			};
		},
		methods:{
			onConfirm(val){
				this.form.city = val.result
			},
			C_submit(){
				
				let {city,address,username,mobile,gender} = this.form;
				let isTel = /^(?:1\d{2})-?\d{5}(\d{3}|\*{3})$/;
				
				if(city == '点击选择地址'){this.$u.toast('点击选择地址');return};
				if(!address){this.$u.toast('请输入详细地址');return};
				if(!username){this.$u.toast('请输入联系人');return};
				if(!isTel.test(mobile)){this.$u.toast('请输入正确的手机号');return};
				
				this.$u.post('appuser/add_address',{
					recive_name		:username,					//是	string	无
					recive_sex		:gender == 0?'1':'2',		//是	int	无
					recive_phone	:mobile,					//是	int	无
					recive_city		:city,						//是	string	无
					recive_address	:address,					//是	string	无
					recive_default	:"2",						//是	int	无     1是  2 否
				}).then(res=>{
					this.$u.toast('保存成功')
					setTimeout(_=>{
						uni.navigateBack()
					},700)
				})
			}
		}
	}
</script>

<style lang="scss">
	.submit{
		text-align: center;
		padding: 15px 0;
	}
	.form{
		background-color: #FFFFFF;
		>view{
			border-bottom: 1px solid rgba(0,0,0,.05);
			padding: 30rpx 50rpx;
			display: flex;
			justify-content: space-between;
			>view{
				display: flex;
				align-items: center;
				.tit{
					margin-right: 20rpx;
				}
				&.sex{
					text{
						border: 1px solid rgba(0,0,0,.1);
						border-radius: 10rpx;
						padding: 10rpx 20rpx;
						margin-left: 20rpx;
						&.active{
							background-color: rgba(36,116,228,.8);
							color: #FFFFFF;
						}
					}
				}
			}
		}
		image{
			width: 50rpx;
			height: 50rpx;
		}
	}
</style>
